package com.sxfq.loansupermarket.app.personal.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.sxfq.loansupermarket.app.constants.AppConstant;
import com.sxfq.loansupermarket.app.dto.AppResponseResult;
import com.sxfq.loansupermarket.app.personal.service.PersonalService;
import com.sxfq.loansupermarket.business.customer.entity.CustomerBaseInfo;
import com.sxfq.loansupermarket.business.customer.entity.CustomerBorrowInfo;
import com.sxfq.loansupermarket.business.customer.entity.CustomerFeedback;
import com.sxfq.loansupermarket.business.customer.entity.CustomerPlatformVisitInfo;
import com.sxfq.loansupermarket.common.annotation.LockAndSyncRequest;
import com.sxfq.loansupermarket.common.annotation.LoginIntercept;
import com.sxfq.loansupermarket.common.utils.IdcardValidator;
import com.sxfq.loansupermarket.common.utils.NumberUtil;
import com.sxfq.loansupermarket.common.utils.StringUtil;

/**
 * 首页控制器
 * 
 * 
 * Module:
 * 
 * PersonalController.java
 * 
 * @author 程盼
 * @since JDK 1.8
 * @version 1.0
 * @description: <描述>
 */
@Controller
@RequestMapping(value = "/personal")
public class PersonalController {

	private Logger logger = Logger.getLogger(PersonalController.class);

	@Resource
	private PersonalService personalService;

	/**
	 * 查询用户借款信息
	 */
	@RequestMapping(value = "getBorrowInfo")
	@ResponseBody
	@LoginIntercept
	public AppResponseResult getBorrowInfo(HttpServletRequest request, HttpServletResponse response) {
		AppResponseResult result = new AppResponseResult();
		try {
			String borrowerId = request.getParameter("borrowerId");// 借款人ID
			logger.info("【PersonalController.getBorrowInfo】borrowerId:" + StringUtil.toString(borrowerId));// 借款信息
			if (StringUtil.isEmpty(borrowerId)) {
				result.setCode(AppConstant.Code.FAIL);
				result.setMsg(AppConstant.Msg.FAIL);
				result.setResult("");
				return result;
			}
			Map<String, Object> map = personalService.getBorrowInfo(borrowerId);
			result.setCode(AppConstant.Code.SUCCESS);
			result.setMsg(AppConstant.Msg.SUCCESS);
			result.setResult(map);
		} catch (Exception e) {
			result.setCode(AppConstant.Code.ERROR);
			result.setMsg(AppConstant.Msg.ERROR);
			result.setResult("");
			logger.error("【PersonalController.getBorrowInfo】异常", e);
		}
		return result;
	}

	/**
	 * 保存用户借款信息
	 */
	@RequestMapping(value = "saveBorrowInfo")
	@ResponseBody
	@LoginIntercept
	@LockAndSyncRequest
	public AppResponseResult saveBorrowInfo(HttpServletRequest request, HttpServletResponse response) {
		AppResponseResult result = new AppResponseResult();
		try {
			String borrowerId = request.getParameter("borrowerId");// 借款人ID
			String expectedAmount = request.getParameter("expectedAmount");// 期望金额
			String borrowCycle = request.getParameter("borrowCycle");// 借款周期
			String paymentDate = request.getParameter("paymentDate");// 到账时间
			logger.info("【PersonalController.saveBorrowInfo】borrowerId:" + StringUtil.toString(borrowerId)
					+ ",expectedAmount:" + StringUtil.toString(expectedAmount) + ",borrowCycle:"
					+ StringUtil.toString(borrowCycle) + ",paymentDate:" + StringUtil.toString(paymentDate));// 借款信息
			if (StringUtil.isEmpty(borrowerId) || StringUtil.isEmpty(expectedAmount) || StringUtil.isEmpty(borrowCycle)
					|| StringUtil.isEmpty(paymentDate)) {
				result.setCode(AppConstant.Code.FAIL);
				result.setMsg(AppConstant.Msg.FAIL);
				result.setResult("");
				return result;
			}
			CustomerBorrowInfo entity = new CustomerBorrowInfo();
			entity.setBorrowerId(Long.parseLong(borrowerId));
			entity.setExpectedAmount(Double.valueOf(expectedAmount));
			entity.setBorrowCycle(StringUtil.toInteger(borrowCycle));
			entity.setPaymentDate(StringUtil.toInteger(paymentDate));
			personalService.saveBorrowInfo(entity);
			result.setCode(AppConstant.Code.SUCCESS);
			result.setMsg(AppConstant.Msg.SUCCESS);
			result.setResult("");
		} catch (Exception e) {
			result.setCode(AppConstant.Code.ERROR);
			result.setMsg(AppConstant.Msg.ERROR);
			result.setResult("");
			logger.error("【PersonalController.saveBorrowInfo】异常", e);
		}
		return result;
	}

	/**
	 * 保存用户浏览记录
	 */
	@RequestMapping(value = "saveCustomerPlatformVisitInfo")
	@ResponseBody
	public AppResponseResult saveCustomerPlatformVisitInfo(HttpServletRequest request, HttpServletResponse response) {
		AppResponseResult result = new AppResponseResult();
		try {
			String borrowerId = request.getParameter("borrowerId");// 借款人ID
			String platformId = request.getParameter("platformId");// 平台ID
			String type = request.getParameter("type");// 访问类型
			logger.info(
					"【PersonalController.saveCustomerPlatformVisitInfo】borrowerId:" + StringUtil.toString(borrowerId)
							+ ",platformId:" + StringUtil.toString(platformId) + ",type:" + StringUtil.toString(type));// 借款信息
			if (StringUtil.isEmpty(type)) {
				result.setCode(AppConstant.Code.FAIL);
				result.setMsg(AppConstant.Msg.FAIL);
				result.setResult("");
				return result;
			}
			CustomerPlatformVisitInfo entity = new CustomerPlatformVisitInfo();
			if (!StringUtil.isEmpty(borrowerId)) {
				entity.setBorrowerId(Long.parseLong(borrowerId));
			}
			if (!StringUtil.isEmpty(platformId)) {
				entity.setPlatformId(Long.parseLong(platformId));
			}
			entity.setType(StringUtil.toInteger(type));
			personalService.saveCustomerPlatformVisitInfo(entity);
			result.setCode(AppConstant.Code.SUCCESS);
			result.setMsg(AppConstant.Msg.SUCCESS);
			result.setResult("");
		} catch (Exception e) {
			result.setCode(AppConstant.Code.ERROR);
			result.setMsg(AppConstant.Msg.ERROR);
			result.setResult("");
			logger.error("【PersonalController.saveCustomerPlatformVisitInfo】异常", e);
		}
		return result;
	}

	/**
	 * 获取用户浏览产品记录
	 */
	@RequestMapping(value = "getCustomerPlatformVisitInfo")
	@ResponseBody
	public AppResponseResult getCustomerPlatformVisitInfo(HttpServletRequest request, HttpServletResponse response) {
		AppResponseResult result = new AppResponseResult();
		try {
			String borrowerId = request.getParameter("borrowerId");// 借款人ID
			Integer pageNum = NumberUtil.parseInteger(request.getParameter("pageNum"), null);// 页码
			Integer pageSize = NumberUtil.parseInteger(request.getParameter("pageSize"), null);// 每页数量
			logger.info("【PersonalController.getCustomerPlatformVisitInfo】borrowerId:" + StringUtil.toString(borrowerId)
					+ ",pageNum:" + StringUtil.toString(pageNum) + ",pageSize:" + StringUtil.toString(pageSize));
			if (StringUtil.isEmpty(borrowerId) || pageNum == null || pageSize == null || pageNum <= 0
					|| pageSize <= 0) {
				result.setCode(AppConstant.Code.FAIL);
				result.setMsg(AppConstant.Msg.FAIL);
				result.setResult("");
				return result;
			}
			List<Map<String, Object>> list = personalService.getCustomerPlatformVisitInfo(borrowerId, pageNum,
					pageSize);
			logger.info("【PersonalController.getCustomerPlatformVisitInfo】list:" + JSONArray.toJSONString(list));
			result.setCode(AppConstant.Code.SUCCESS);
			result.setMsg(AppConstant.Msg.SUCCESS);
			result.setResult(list);
		} catch (Exception e) {
			result.setCode(AppConstant.Code.ERROR);
			result.setMsg(AppConstant.Msg.ERROR);
			result.setResult("");
			logger.error("【PersonalController.getCustomerPlatformVisitInfo】异常", e);
		}
		return result;
	}

	/**
	 * 查询我的资料
	 */
	@RequestMapping(value = "getCustomerBaseInfo")
	@ResponseBody
	@LoginIntercept
	public AppResponseResult getCustomerBaseInfo(HttpServletRequest request, HttpServletResponse response) {
		AppResponseResult result = new AppResponseResult();
		try {
			String borrowerId = request.getParameter("borrowerId");// 借款人ID
			if (StringUtil.isEmpty(borrowerId)) {
				result.setCode(AppConstant.Code.FAIL);
				result.setMsg(AppConstant.Msg.FAIL);
				result.setResult("");
				return result;
			}
			logger.info("【PersonalController.getCustomerBaseInfo】borrowerId:" + StringUtil.toString(borrowerId));
			Map<String, Object> map = personalService.getCustomerBaseInfo(borrowerId);
			result.setCode(AppConstant.Code.SUCCESS);
			result.setMsg(AppConstant.Msg.SUCCESS);
			result.setResult(map);
		} catch (Exception e) {
			result.setCode(AppConstant.Code.ERROR);
			result.setMsg(AppConstant.Msg.ERROR);
			result.setResult("");
			logger.error("【PersonalController.getCustomerBaseInfo】异常", e);
		}
		return result;
	}

	/**
	 * 保存我的资料
	 */
	@RequestMapping(value = "saveCustomerBaseInfo")
	@ResponseBody
	@LoginIntercept
	@LockAndSyncRequest
	public AppResponseResult saveCustomerBaseInfo(HttpServletRequest request, HttpServletResponse response) {
		AppResponseResult result = new AppResponseResult();
		try {
			String borrowerId = request.getParameter("borrowerId");// 借款人ID
			String name = request.getParameter("name");// 真实姓名
			String idCard = request.getParameter("idCard");// 身份证号
			logger.info("【PersonalController.saveCustomerBaseInfo】borrowerId:" + borrowerId + "name:"
					+ StringUtil.toString(name) + ",idCard:" + idCard);
			if (StringUtil.isEmpty(borrowerId) || StringUtil.isEmpty(name) || StringUtil.isEmpty(idCard)) {
				result.setCode(AppConstant.Code.FAIL);
				result.setMsg(AppConstant.Msg.FAIL);
				result.setResult("");
				return result;
			}
			if (!IdcardValidator.isValidatedAllIdcard(idCard)) {
				result.setCode(AppConstant.Code.FAIL);
				result.setMsg("身份证格式错误");
				result.setResult("");
				return result;
			}
			CustomerBaseInfo entity = new CustomerBaseInfo();
			entity.setBorrowerId(Long.parseLong(borrowerId));
			entity.setName(name);
			entity.setIdCard(idCard);
			personalService.saveCustomerBaseInfo(entity);
			result.setCode(AppConstant.Code.SUCCESS);
			result.setMsg(AppConstant.Msg.SUCCESS);
			result.setResult("");
		} catch (Exception e) {
			result.setCode(AppConstant.Code.ERROR);
			result.setMsg(AppConstant.Msg.ERROR);
			result.setResult("");
			logger.error("【PersonalController.saveCustomerBaseInfo】异常", e);
		}
		return result;
	}

	/**
	 * 保存用户反馈信息
	 */
	@RequestMapping(value = "saveCustomerFeedBack")
	@ResponseBody
	@LoginIntercept
	@LockAndSyncRequest
	public AppResponseResult saveCustomerFeedBack(HttpServletRequest request, HttpServletResponse response) {
		AppResponseResult result = new AppResponseResult();
		try {
			String borrowerId = request.getParameter("borrowerId");// 借款人ID
			String type = request.getParameter("type");// 反馈问题类型
			String content = request.getParameter("content");// 反馈内容
			String reservePhone = request.getParameter("reservePhone");// 预留手机号
			logger.info("【PersonalController.saveCustomerBaseInfo】borrowerId:" + StringUtil.toString(borrowerId)
					+ ",type:" + StringUtil.toString(type) + ",content:" + StringUtil.toString(content)
					+ ",reservePhone:" + StringUtil.toString(reservePhone));
			if (StringUtil.isEmpty(borrowerId) || StringUtil.isEmpty(type) || StringUtil.isEmpty(content)
					|| StringUtil.isEmpty(reservePhone)) {
				result.setCode(AppConstant.Code.FAIL);
				result.setMsg(AppConstant.Msg.FAIL);
				result.setResult("");
				return result;
			}
			CustomerFeedback entity = new CustomerFeedback();
			entity.setBorrowerId(Long.parseLong(borrowerId));
			entity.setType(StringUtil.toInteger(type));
			entity.setContent(content);
			entity.setReservePhone(reservePhone);
			personalService.saveCustomerFeedBack(entity);
			result.setCode(AppConstant.Code.SUCCESS);
			result.setMsg(AppConstant.Msg.SUCCESS);
			result.setResult("");
		} catch (Exception e) {
			result.setCode(AppConstant.Code.ERROR);
			result.setMsg(AppConstant.Msg.ERROR);
			result.setResult("");
			logger.error("【PersonalController.saveCustomerFeedBack】异常", e);
		}
		return result;
	}

}
